package com.econ.powercloud.ems.timing.compute.strategy;

import com.econ.powercloud.ems.common.enums.ComputeModel;
import com.econ.powercloud.ems.dal.entity.EmsComputeConfig;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description : 计算数据单元的入口
 * @Date : 2022/11/25 11:29
 * @Modified by : zjf
 */
@Component
public class ComputeCodeStrategy {

    @Autowired
    private List<AbstractComputeCodeProcess> computeCodeProcessList;

    private Map<Integer, AbstractComputeCodeProcess> processMap = Maps.newHashMap();

    @PostConstruct
    private void init() {
        processMap = computeCodeProcessList.stream().collect(Collectors.toMap(AbstractComputeCodeProcess::getComputeUnitId, item -> item));
    }

    public Double compute(EmsComputeConfig emsComputeConfig,Map<String,Double> hasCalculateVal,Long startTime){
        if(emsComputeConfig == null){
            return null;
        }
        ComputeModel computeModel = ComputeModel.fromModel(emsComputeConfig.getComputeModel());
        if(computeModel.getCal() == null){
            return null;
        }
        AbstractComputeCodeProcess process = processMap.get(computeModel.getCal());
        if (process == null) {
            return null;
        }
        Double result = process.compute(emsComputeConfig,hasCalculateVal,startTime);
        return result;
    }
}
